<?php
namespace app\admin\controller;
use think\Db;
class Sendgoods extends Base
{
    public function lists(){
        if(is_post()){
            $is_down = input('is_down');
            $cur_page = input('page');
            $status = input('status');
            $keyword  = input('keyword');
            $sql = "
            select
                [1]
            from
                order_goods_detail a
            left join
                order_goods_fahuo b
            on
                a.order_goods_fahuo_id=b.id
            left join
                express_mb c
            on
                b.express_mb_id=c.id
            ";
            $param = [];
            $w = ['where'];
            $w[] = ' and a.is_sq=?';
            $param[] = 1;
            if($status!='100all'){
                $w[] = ' and a.status=?';
                $param[] = $status;
            }
            if(!empty($keyword)){
                $w[] = ' and (sh_name like ? or sh_phone like ? or sh_address like ?)';
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*,b.kd_no,c.name";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= $is_down ? " order by a.id desc" : " order by a.id asc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['pic'] = '<img src="'.$v['pic'].'" height=50 />';
                $rslist[$k]['status'] = $v['status']?'已发货':'未发货';
                $rslist[$k]['kd_no'] = "{$v['name']}:{$v['kd_no']}";
                $rslist[$k]['action'] = '';
                if($v['status']==0){
                    $rslist[$k]['action']  .= com_btn_edit("/sendgoods/edit/name/{$v['sh_name']}/phone/{$v['sh_phone']}/address/{$v['sh_address']}",'','','发货');
                }
            }
            if($is_down){
                $url = com_down_csv($rslist,
                ['商品','规格','数量','收货姓名','收货电话','收货地址','快递信息','状态'],
                ['title','spec','num','sh_name','sh_phone','sh_address','kd_no','status'],'fahuo','xls');
                com_check(['data'=>$url,'suc_js'=>'function.openurl']);
            }
            $body_html = com_list_body($rslist, ['id','pic','title','spec','num','sh_name','sh_phone','sh_address','kd_no','status','action']);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        return view();
    }
    //发货
    public function fahuo(){
        if(is_post()){
            $d = input();
            $rule = [
                'ids'=>  'require',
                'kd_no'=>  'require',
                'express_mb_id'=>  'require',
                'note'=>  'require',
            ];
            $msg = [
            ];
            com_validate($rule, $msg, $d);
            $rs = Db::name('order_goods_fahuo')->where('express_mb_id',$d['express_mb_id'])->where('kd_no',$d['kd_no'])->find();
            if($rs && $d['kd_no']!='0000'){
                com_error('已存在的快递单号','kd_no');
            }else{
                $express_name = Db::name('express_mb')->where('id',$d['express_mb_id'])->value('name');
                $id = Db::name('order_goods_fahuo')->insert([
                    'kd_no' => $d['kd_no'],
                    'express_mb_id' => $d['express_mb_id'],
                    'express_name' => $express_name,
                    'note' => $d['note'],
                    'create_time' => time()
                ]);
                Db::name('order_goods_detail')->where('id','in',$d['ids'])->update(['order_goods_fahuo_id'=>$id,'status'=>1]);
                $rs = Db::query("select
                c.name,a.sh_phone,b.order_no
                from
                    order_goods_detail a
                left join
                    order_goods b
                on
                    a.order_goods_id=b.id
                left join
                    user c
                on
                    a.user_id=c.id
                where a.id=?",[$d['ids'][0]]);
                $info = $rs[0];
                #尊敬的#user#,您在商城的订单#order#已发货，物流信息为#info#。 1843160
                #短信
                if($d['kd_no']!='0000'){
                    $sms = new \app\common\model\Sms();
                    $content = "尊敬的#user#,您在商城的订单#order#已发货，物流信息为#info#";
                    $tpl_val = json_encode(["user" =>$info['name'],"order"=>$info['order_no'],"info"=>$express_name.":".$d['kd_no']]);
                    $id = $sms->create($info['sh_phone'], $content, 0, '', 1843160,$tpl_val);
                    $sms->sendOne($id);
                }
                com_check(['suc_msg'=>'发货成功!','suc_js'=>'alert|f5']);
            }
        }
    }
    public function edit($name,$phone,$address){
        $list = Db::name('order_goods_detail')->where('sh_name',$name)
                                            ->where('sh_phone',$phone)
                                            ->where('sh_address',$address)
                                            ->where('is_sq',1)
                                            ->where('status',0)
                                            ->select();
        $express = Db::name('express_mb')->where('status',0)->order('sort desc,id desc')->select();
        return view('',['list'=>$list,'name'=>$name,'phone'=>$phone,'address'=>$address,'express'=>$express]);
    }
}
